package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.bean.PageBean;
import com.tiantian.cache.user.UserInfoCache;
import com.tiantian.cache.user.UserInfoImpl;
import com.tiantian.dao.BackstagelogRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.BackstageRole;
import com.tiantian.domain.Backstagelog;
import com.tiantian.service.BackstagelogService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

@Service
public class BackstagelogServiceImp extends GenericServiceImpl<Backstagelog, String> implements BackstagelogService {
	private BackstagelogRepository repository;

	private static Map<String, ModelInfo> cache = new HashMap<String, ModelInfo>();

	@PostConstruct
	public void init() {
		// 审核
		cache.put("010", new ModelInfo("010", "审核买手", "审核买手", "申请"));
		cache.put("011", new ModelInfo("011", "审核买手绑定任务平台账号", "审核买手绑定任务平台账号", "申请"));
		cache.put("012", new ModelInfo("012", "审核商家绑定商铺", "审核商家绑定商铺", "申请"));
		cache.put("013", new ModelInfo("013", "审核买号", "审核买号", "申请"));
		// 任务
		cache.put("02", new ModelInfo("02", "任务管理", "撤销未接单任务", ""));
		// 订单
		cache.put("03", new ModelInfo("03", "订单管理", "撤销订单", ""));
		// 申述
		cache.put("041", new ModelInfo("041", "申述管理", "跟进订单", "申述"));
		cache.put("042", new ModelInfo("042", "申述管理", "完结订单", "申诉"));
		cache.put("043", new ModelInfo("043", "申述管理", "回复订单", "申述"));
		// 资金管理
		cache.put("051", new ModelInfo("051", "商家充值管理", "审批商家", "申请"));
		cache.put("052", new ModelInfo("052", "买手体现管理", "审批买手", "申请"));
		cache.put("053", new ModelInfo("053", "管理员人工划款", "平台向买手", ""));
		cache.put("054", new ModelInfo("054", "管理员人工划款", "平台向商家", ""));
	}

	public static class ModelInfo {
		private String model;
		private String modelName;
		private String before;
		private String end;

		public ModelInfo(String model, String modelName, String before, String end) {
			super();
			this.model = model;
			this.modelName = modelName;
			this.before = before;
			this.end = end;
		}

		public String getDesc(String relationId, String msg) {
			String ret = before.concat(relationId);
			if (StringUtils.isNotBlank(msg)) {
				return ret.concat(msg).concat(end);
			}
			return ret.concat(end);
		}

		public String getModel() {
			return model;
		}

		public String getModelName() {
			return modelName;
		}

	}

	@Autowired
	public void setRepository(BackstagelogRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	@Override
	public void log(String model, String relationId, String msg, boolean result) {
		Backstagelog log = new Backstagelog();
		log.setLogStatus(result ? "1" : "0");
		log.setModel(model);
		log.setRelationId(relationId);
		ModelInfo modelInfo = cache.get(model);
		log.setModelName(modelInfo.getModelName());
		log.setLogDesc(modelInfo.getDesc(relationId, msg));
		log.setCreatetime(System.currentTimeMillis());
		UserInfoImpl userInfo = (UserInfoImpl) UserInfoCache.getCurrentUser();
		log.setIp(userInfo.getIpAddress());
		log.setUsername(userInfo.getUserName());
		log.setRolenames(getRoles(userInfo.getRoles()));
		this.save(log);
	}

	private String getRoles(List<BackstageRole> roles) {
		if (roles != null) {
			int length = roles.size() > 3 ? 3 : roles.size();
			String ret = null;
			for (int i = 0; i < length; i++) {
				if (i == 0) {
					ret = roles.get(i).getName();
				} else {
					ret += "," + roles.get(i).getName();
				}
			}
			return ret;
		}
		return null;
	}

	@Override
	public List<Backstagelog> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.LIST);
		List<Backstagelog> ret = repository.queryListBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams(), Backstagelog.class);
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getQueryCondition(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.* ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from backstagelog t where 1=1 ");

		if (searchMap != null) {

		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.createtime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	@Override
	public void log(String model, String relationId, boolean result) {
		log(model, relationId, null, result);
	}

}
